package com.inputabc.ct.v1.util;

import com.baidu.translate.demo.TransApi;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaiduUtils {
    // 百度API密钥
    public static String APP_ID = "";
    public static String APP_SECRET = "";
    
    public static final String CHINESE = "zh";
    public static final String ENGLISH = "en";
    public static final String JPANESE = "ja";
    public static final String KOREAN = "ko";
    public static final String AUTO = "auto";

    public static String translate(String src, String srcLang, String targetLang, String app_id, String security_key) {
        TransApi api = new TransApi(app_id, security_key);
        String transResult = api.getTransResult(src, srcLang, targetLang);
        JSONObject fromObject = JSONObject.fromObject(transResult);
        Map<String, Object> map = (Map<String, Object>) JSONObject.toBean(fromObject, HashMap.class);
        String value;
        try {
            List<MorphDynaBean> list = (List) map.get("trans_result");
            MorphDynaBean bean = list.get(0);
            value = (String) bean.get("dst");
        } catch (Exception e) {
            e.printStackTrace();
            return transResult;
        }
        return value;
    }
}
